<?php


namespace app\admin\controller;

use app\common\controller\CommonService;
use think\facade\Validate;
use think\Controller;
use think\Request;
use think\Db;

class Recommend extends Controller
{
    /**
     * 广告列表(增加条目名称字段 adver_little，增加广告位状态撤销字段：revoke 0 未撤销 ， 1 撤销，增加创建时间字段 time，增加创建人字段 admin_id)
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function advertList(Request $request)
    {
        $requestData = $request->post();
        $db = Db::name('adver')
            ->alias('a')
            ->leftJoin('admin_manage m');
        if (!empty($requestData['adver_little'])){
            $db = $db->where('adver_little','like','%'.$requestData['adver_little']);
        }
        if (!empty($requestData['type'])){
            $db = $db->where('type',$requestData['type']);
        }
        if (!empty($requestData['flag'])){
            $db = $db->where('flag',$requestData['flag']);
        }
        if (!empty($requestData['revoke'])){
            $db = $db->where('revoke',$requestData['revoke']);
        }
        if (!empty($requestData['start_time_start']) && !empty($requestData['start_time_end'])){
            $db = $db->whereBetween('start_show_time',[$requestData['start_time_start'],$requestData['start_time_end']]);
        }
        if (!empty($requestData['end_time_start']) && !empty($requestData['end_time_end'])){
            $db = $db->whereBetween('end_show_time',[$requestData['end_time_start'],$requestData['end_time_end']]);
        }
        if (!empty($requestData['start_time']) && !empty($requestData['end_time'])){
            $db = $db->whereBetween('time',[$requestData['start_time'],$requestData['end_time']]);
        }
        if (!empty('username')){
            $adminId = Db::name('admin_manage')->where('username',$requestData['username'])->value('id');
            $db = $db->where('admin_id',$adminId);
        }
        if (!empty($requestData['order_type']) && !empty($requestData['sort_type'])){
            $db = $db->order($requestData['order_type'],$requestData['sort_type']);
        }else {
            $db = $db->order('a.id','desc');
        }
        $db1 = clone $db;
        $data['data'] = $db
            ->field('a.id,a.adver_little,a.img_url,a.type,a.flag,a.title,a.start_show_time,a.end_show_time,a.time,m.username')
            ->page(input('page',1),input('limit',20))
            ->select();
        $data['total_num'] = $db1->count();
    }

    /**
     * 新增广告
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function addAdvert(Request $request)
    {
        $adminId = $request->adminId;
        $requestData = $request->post();
        $validate = Validate::make([
            'type|类型' => 'require',
            'title|广告名称' => 'require',
            'img_url|展示图片' => 'require|url',
            'sort|排序' => 'require|number',
            'flag|链接类型' => 'require|number',
            'start_show_time|开始时间' => 'require',
            'end_show_time|结束时间' => 'require',
        ]);
        if (!$validate->check($requestData)) {
            return CommonService::error($validate->getError());
        }
        $adminInfo = Db::name('admin_manage')->where('id',$adminId)->find();
        if (empty($adminInfo)){
            return CommonService::error(0);
        }
        $result = Db::name('advert')->insert([
            'type' => $requestData['type'],
            'tittle' => $requestData['tittle'],
            'img_url' => $requestData['img_url'],
            'sort' => $requestData['sort'],
            'flag' => $requestData['flag'],
            'start_show_time' => $requestData['start_show_time'],
            'end_show_time' => $requestData['end_show_time'],
            'time' => time()
        ]);
        if (!$result){
            return CommonService::success([]);
        }
    }
    /**
     * 撤销广告
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
     public function revokeAdvert(Request $request)
     {

     }

    /**
     * 上传广告位
     * @return \think\response\Json
     * @throws \OSS\Core\OssException
     */
    public function uploadAdvertImg()
    {
        $requestData = request()->post();
        $validate = Validate::make([
            'super_partner_id' => 'require',
        ]);
        if (!$validate->check($requestData)) {
            return CommonService::error($validate->getError());
        }
        $files = request()->file();
        foreach ($files as $file) {
            if (!$file->check(['size' => 1024000 * 5, 'ext' => 'jpg,jpeg,png,gif'])) {
                return CommonService::error($file->getError());
            }
        }
        switch ($files) {
            case !empty($files['advert']):
                $file = $files['advert'];
                $fileName = $file->getinfo('name');
                $fileName = explode(".", $fileName);
                $fileExt = trim(array_pop($fileName));
                $object = "static/admin/advert{$requestData['super_partner_id']}.{$fileExt}";
                break;
            default:
                return CommonService::error(15);
        }
        $url = CommonService::uploadOss($file->getinfo('tmp_name'), $object);
        if ($url) {
            return CommonService::success(['url' => $url . '?' . time()]);
        } else {
            return CommonService::error(15);
        }
    }
}